/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.adapter;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.bean.WeekChooseBean;

import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * Created by Liu ZhuangFei on 2018/6/18. BaseAdapter
 *
 * @since 2021-03-29
 */
public class ChooseAdapter extends BaseItemProvider {
    ArrayList<WeekChooseBean> datas;
    LayoutScatter inflater;
    private CheckListen checkListen;

    /**
     * 构造方法
     *
     * @param context
     * @param data
     */
    public ChooseAdapter(Context context, ArrayList<WeekChooseBean> data) {
        this.datas = data;
        inflater = LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component convertView, ComponentContainer viewGroup) {
        convertView = inflater.parse(ResourceTable.Layout_item_choose, null, false);
        RadioButton mRbCheck = (RadioButton) convertView.findComponentById(ResourceTable.Id_rb_check);
        Text mTvCheck = (Text) convertView.findComponentById(ResourceTable.Id_tv_check);

        WeekChooseBean data = datas.get(i);

        mTvCheck.setText(data.getWeek());

        if (data.isSelect()) {
            mRbCheck.setChecked(true);
        } else {
            mRbCheck.setChecked(false);
        }

        convertView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                int index = i;
                for (int ii = 0; ii < datas.size(); ii++) {
                    if (index == ii) {
                        datas.get(ii).setSelect(true);
                    } else {
                        datas.get(ii).setSelect(false);
                    }
                }
                notifyDataChanged();
                checkListen.check(i);
            }
        });
        return convertView;
    }

    public void setCheckListen(CheckListen listen) {
        this.checkListen = listen;
    }

    /**
     * CheckListen
     *
     * @since 2021-04-22
     */
    public interface CheckListen {
        /**
         * 回调参数
         *
         * @param index
         */
        void check(int index);
    }
}
